package com.kevin.util.tag;

import java.lang.reflect.Method;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class PageTag  extends TagSupport{
	private Object pageInfo = new Object();
	private Integer nowpn = 1;
	private Integer totalpn;
	private Boolean jumppage = true;
	
	
	public Object getPageInfo() {
		return pageInfo;
	}

	public void setPageInfo(Object pageInfo) {
		this.pageInfo = pageInfo;
	}
	
	public Integer getNowpn() {
		return nowpn;
	}

	public void setNowpn(Integer nowpn) {
		this.nowpn = nowpn;
	}

	public Integer getTotalpn() {
		return totalpn;
	}

	public void setTotalpn(Integer totalpn) {
		this.totalpn = totalpn;
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 134546456L;
	

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public int doEndTag() throws JspException {
		// TODO Auto-generated method stub
		
		try {	
			StringBuffer newStr = new StringBuffer();
			Class cla = pageInfo.getClass();
			Method getNext = cla.getMethod("getNext");
			Object next = getNext.invoke(pageInfo);
			Method getLastPage = cla.getMethod("getLastPage");
			Object lastPage = getLastPage.invoke(pageInfo);
			Method getPrevious = cla.getMethod("getPrevious");
			Object previous = getPrevious.invoke(pageInfo);
			Method getFirstPage = cla.getMethod("getFirstPage");
			Object firstPage = getFirstPage.invoke(pageInfo);
			Method isFirst = cla.getMethod("isFirst");
			Boolean first = (Boolean)isFirst.invoke(pageInfo);
			Method isLast = cla.getMethod("isLast");
			Boolean last = (Boolean)isLast.invoke(pageInfo);
			
			if(first){
				newStr.append("<a class=\"gray\">首页</a>\n<a class=\"gray\">上页</a>");
			}else{
				newStr.append("<a href=\""+firstPage+"\">首页</a>\n<a href=\""+previous+"\">上页</a>");
			}
			if(totalpn <=5){
				for(int i=0;i<totalpn;i++){
					String pageUrl = (firstPage+"").replace("p=1", "p="+(i+1));
					if(nowpn==(i+1)){
						newStr.append("\n<strong>"+(i+1)+"</strong>");
					}else{
						newStr.append("\n<a href=\""+pageUrl+"\">"+(i+1)+"</a>");						
					}
				}
			}else{
				if(nowpn - 2 <=0){
					for(int i=0;i<5;i++){
						String pageUrl = (firstPage+"").replace("p=1", "p="+(i+1));
						if(nowpn==(i+1)){
							newStr.append("\n<strong>"+(i+1)+"</strong>");
						}else{
							newStr.append("\n<a href=\""+pageUrl+"\">"+(i+1)+"</a>");							
						}
					}
				}else if(totalpn - nowpn  <=2){
					for(int i=totalpn-5;i<totalpn;i++){
						String pageUrl = (firstPage+"").replace("p=1", "p="+(i+1));
						if(nowpn==(i+1)){
							newStr.append("\n<strong>"+(i+1)+"</strong>");
						}else{
							newStr.append("\n<a href=\""+pageUrl+"\">"+(i+1)+"</a>");							
						}
					}
				}else{
					for(int i=nowpn-2;i<=nowpn+2;i++){
						String pageUrl = (firstPage+"").replace("p=1", "p="+(i));
						if(nowpn==(i)){
							newStr.append("\n<strong>"+i+"</strong>");
						}else{
							newStr.append("\n<a href=\""+pageUrl+"\">"+i+"</a>");							
						}
					}
				}
			}			
			if(last){
				newStr.append("\n<a class=\"gray\">下页</a>\n<a class=\"gray\">末页</a>");
			}else{
				newStr.append("\n<a href=\""+next+"\">下页</a>\n<a href=\""+lastPage+"\">末页</a>");
			}
			if(jumppage){
				newStr.append("\n到第<input type=\"text\" id=\"pageno1\" class=\"page_input\" onKeyDown=\"if(event.keyCode==13) {window.location='"+(firstPage+"").replace("p=1", "")+"&p='+this.value; return false;}\"/>页")
					.append("\n<input type=\"button\" class=\"go\" style=\"cursor: pointer;\" onclick=\"jumppage('"+totalpn+"','"+(firstPage+"").replace("p=1", "")+"')\"/>");
			}
			pageContext.getOut().print(newStr);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SKIP_BODY;
	}

	@Override
	public int doStartTag() throws JspException {
		// TODO Auto-generated method stub
		return EVAL_PAGE;
	}

	public Boolean getJumppage() {
		return jumppage;
	}

	public void setJumppage(Boolean jumppage) {
		this.jumppage = jumppage;
	}
	
}
